package com.ejlchina.keno

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.ejlchina.keno.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        windowInset()
        binding.btnSubmit.setOnClickListener {
            if (checkValidate()) {
                check(getResultIntList())
            }
        }
    }

    private fun windowInset() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun getResultStringList() :List<String>{
        return listOf(
            binding.etFirst.text.toString(),
            binding.etSecond.text.toString(),
            binding.etThird.text.toString(),
            binding.etForth.text.toString(),
            binding.etFifth.text.toString())
    }

    private fun getResultIntList() :List<Int> {
        return getResultStringList().map { it.toInt() }
    }

    private fun checkValidate(): Boolean {
        if (getResultStringList().any { it.isBlank() }) {
            Toast.makeText(this,"数字没有填写完整",Toast.LENGTH_SHORT).show()
            return false
        }

        val resultIntList = getResultIntList()

        if (resultIntList.any { it > 35 || it < 1 }) {
            Toast.makeText(this,"数字大小不正确",Toast.LENGTH_SHORT).show()
            return false
        }

        if (!resultIntList.isAscending()) {
            Toast.makeText(this,"数字顺序不正确",Toast.LENGTH_SHORT).show()
            return false
        }
        return true
    }

    @SuppressLint("SetTextI18n")
    private fun check(resultList: List<Int>) {
        val longPageList = FRONT_NO_REPEAT_NEW.shortToLongList()
        val pageHitCountList = mutableListOf<Int>()
        longPageList.forEachIndexed { index, pageLongList ->
            val count = intersectCount(pageLongList,resultList)
            pageHitCountList.add(count)
            val pageIntersectionList = pageLongList.intersect(resultList.toSet())
            val resultString = "${index + 1}: $pageIntersectionList" + when(pageIntersectionList.size) {
                0 -> " --->No Hit"
                1 -> " ------------>Only One Hit"
                5 -> " --->All Hit"
                else -> ""
            }
            println(resultString)
        }
        val countMap = getAppearCountIntList2(pageHitCountList)
        binding.tvContinuousThreeSame.text = continuousThreeSameCount(FRONT_NO_REPEAT_NEW,resultList,true).toString()
        binding.tvThreeHit.text = checkThreeSameInLine(resultList).toString()
        binding.tvHitCount.text = countMap.toString()
    }

    /*
 平铺列表，然后计算每个数字出现的数量
 */
    private fun getAppearCountIntList2(sourceList: List<Int>): MutableMap<Int, Int>{
        val map = mutableMapOf<Int, Int>()
        map[1] = sourceList.count { it == 1 }
        map[2] = sourceList.count { it == 2 }
        map[3] = sourceList.count { it == 3 }
        map[4] = sourceList.count { it == 4 }
        map[5] = sourceList.count { it == 5 }
        return map
    }
}